package com.enqualcomm.kids.activities;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.config.userdefault.TerminalDefault;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.network.socket.response.QueryUserTerminalInfoResult;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;
import com.enqualcomm.kids.view.CircleImageView;
import com.enqualcomm.kids.view.ImageUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import common.zxing.ZxingUtil;

/**
 * Created by Sunzhigang on 2015/12/22.
 */
@EActivity(R.layout.pager_qrcode)
public class QRCodeActivity extends BaseActivity {
    TerminallistResult.Terminal mTerminal;
    QueryUserTerminalInfoResult.Data info;
    private String imei;

    @ViewById(R.id.qrcode_iv)
    ImageView qrcode_iv;
    @ViewById(R.id.center_head_iv)
    CircleImageView center_head_iv;
    @ViewById(R.id.big_terminal_name_tv)
    TextView big_terminal_name_tv;
    @ViewById(R.id.small_terminal_name_tv)
    TextView small_terminal_name_tv;
    @ViewById(R.id.qrcode_tv)
    TextView qrcode_tv;
    @ViewById(R.id.title_bar_title_tv)
    TextView title_bar_title_tv;
    @ViewById(R.id.title_bar_terminal_name_tv)
    TextView title_bar_terminal_name_tv;

    @AfterViews
    void setupView() {
        mTerminal = getTerminal();
        info = new UserTerminalDefault(mTerminal.userterminalid).getInfo();
        imei = new TerminalDefault(mTerminal.terminalid).getConfig().imei;
        initView();

    }

    @Click(R.id.title_bar_left_iv)
    void goback() {
        finish();
    }

    private void initView() {
//        findViewById(R.id.top_layout).setBackgroundResource(R.color.colorPrimary);
//        title_bar_title_tv.setText(getString(R.string.qrcode_title));
        setupTitleIcon(info, mTerminal.terminalid, mTerminal.userterminalid, getString(R.string.q_r_qrcode_title));
        Bitmap imageBitmap = ImageUtils.getImageBitmap(this, mTerminal.terminalid, mTerminal.userterminalid, info.gender);
        center_head_iv.setImageBitmap(imageBitmap);
        big_terminal_name_tv.setText(info.name);
        small_terminal_name_tv.setText("");

        if (!TextUtils.isEmpty(imei)) {
            try {
                qrcode_iv.setBackgroundDrawable(new BitmapDrawable(ZxingUtil.create2DCode(imei)));
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (imei.length() == 15) {//IMEI的长度是15位
                qrcode_tv.setText("WID:" + imei);
            } else {
                qrcode_tv.setText("CID:" + imei);
            }
        }

    }

}
